package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dto.ShoppingItem;
import service.ShoppingCartManager;
import service.factory.ShoppingCartManagerFactory;
import util.JacksonUtil;

/**
 * 购物车请求操作
 */
public class ShoppingCartServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	private ShoppingCartManager manager;

	public ShoppingCartServlet() {
		manager = ShoppingCartManagerFactory.getInstance();
	}

	@Override
	public void execute(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {

	}

	public void add(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		System.out.println("ADD...............");
		String foodId = req.getParameter("foodId");
		if(foodId==null){
			foodId = (String) req.getAttribute("foodId");
		}
		System.out.println("foodId 是 ： "+foodId);
		Map<String, ShoppingItem> map = (Map<String, ShoppingItem>) req
				.getSession().getAttribute("shoppingcart");
		if (map == null) {
			map = new HashMap<String, ShoppingItem>();
		}
		map = this.manager.addShoppingCartItem(map, foodId);
		req.getSession().setAttribute("shoppingcart", map);

		List<ShoppingItem> resList = new ArrayList<ShoppingItem>();
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			resList.add(map.get(key));
		}
		String jsonStr = JacksonUtil.fromObjectToJson(resList);
		System.out.println("返回的购物车是： " + jsonStr);
		PrintWriter writer = resp.getWriter();
		writer.write(jsonStr);
		writer.close();
	}

	public void delete(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		String foodId = req.getParameter("foodId");
		if(foodId==null){
			foodId = (String) req.getAttribute("foodId");
		}
		Map<String, ShoppingItem> map = (Map<String, ShoppingItem>) req
				.getSession().getAttribute("shoppingcart");
		if (map == null) {
			map = new HashMap<String, ShoppingItem>();
		}
		map = this.manager.deleteShoppingCartItem(map, foodId);
		req.getSession().setAttribute("shoppingcart", map);

		List<ShoppingItem> resList = new ArrayList<ShoppingItem>();
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			resList.add(map.get(key));
		}
		String jsonStr = JacksonUtil.fromObjectToJson(resList);
		System.out.println("返回的购物车是： " + jsonStr);
		PrintWriter writer = resp.getWriter();
		writer.write(jsonStr);
		writer.close();
	}

	public void clear(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
		String foodId = req.getParameter("foodId");
		if(foodId==null){
			foodId = (String) req.getAttribute("foodId");
		}

		Map<String, ShoppingItem> map = (Map<String, ShoppingItem>) req
				.getSession().getAttribute("shoppingcart");
		if (map == null) {
			map = new HashMap<String, ShoppingItem>();
		}
		this.manager.clearShoppingCart(map);
		
		req.getSession().setAttribute("shoppingcart", new HashMap<String, ShoppingItem>());

		List<ShoppingItem> resList = new ArrayList<ShoppingItem>();
		String jsonStr = JacksonUtil.fromObjectToJson(resList);
		System.out.println("返回的购物车是： " + jsonStr);
		PrintWriter writer = resp.getWriter();
		writer.write(jsonStr);
		writer.close();
	}
}
